<?php
namespace app\frontend\controller;
use think\Db;
use think\Paginator;
use think\Controller;
class Scenic extends Controller
{
    public function index()
    {
      if(!session('user_id'))
      {
        $user_name='登录';
            $login='/frontend/login/index';
        $user_register='注册';
        $user_logout='';
      }
      else
      {
            if(!session('user_name'))
          {
            $user_name=session('user_phone');
          }
          else
          {
            $user_name=session('user_name');
          }
            $login='/frontend/person/index';
            $user_register='欢迎你';
            $user_logout='退出';
      }

      $this->assign('login',$login);
      $this->assign('user_name',$user_name);
      $this->assign('user_register',$user_register);
      $this->assign('user_logout',$user_logout);

      $user_id=session('user_id');
    	$list=db('product')->where("product_sort='0'")->paginate(5);
      $user_score=db('user')->where("user_id='{$user_id}'")->field('user_score')->select();

        foreach ($user_score as $value) 
        { 
          $user_score=$value['user_score'];
        }

        if($user_score>=50 and $user_score<100){
          //$priceTotal=$price_total*0.95;
          $user_score='0.95';

        } 
        else if ($user_score>=100 and $user_score<150) {
          //$priceTotal=$$price_total*0.9;
          $user_score='0.9';
        }
        else if ($user_score>=150) {
          //$priceTotal=$$price_total*0.8;
          $user_score='0.8';
        }
        else
        {
          //$priceTotal=$$price_total;
          $user_score='1';
        }
        foreach ($list as $value) 
        {
            $product_id[]=$value['product_id'];
            //$product_img[]=$value['product_img'];
        	  $product_name[]=$value['product_name'];
            $product_num[]=$value['product_number'];
            $product_price[]=$value['product_price'];
            $product_count[]=$value['product_count'];
            $product_detail[]=$value['product_detail'];         
        }
       
    	$this->assign('list',$list);
      $this->assign('user_score',$user_score);
    	return view('index/scenic');
    }
  

    public function shopcart()
    {
        $user_id=session('user_id');
        $id=$_POST['id'];
        $count=$_POST['count'];
        $priceTotal=$_POST['priceTotal'];
        $date=$_POST['date'];

        $list=db('shopcart')->where("user_id='{$user_id}' and start_time='{$date}' and product_number='{$id}'")->select();
        if(count($list)==0)
        {
          $data=[
            'user_id'=>$user_id,
            'product_number'=>$id,
            'product_amount'=>$count,
            'start_time'=>$date,
            'product_subtotal'=>$priceTotal,
            'shopcart_time'=>time()
          
         ];

          $ret=db('shopcart')->insert($data);
        }
        else
        {
          $product_amount=db('shopcart')->where("user_id='{$user_id}' and start_time='{$date}' and product_number='{$id}'")->setInc('product_amount',$count);
          $product_total=db('shopcart')->where("user_id='{$user_id}' and start_time='{$date}' and product_number='{$id}'")->setInc('product_subtotal',$priceTotal);

        }


        return true;
    }
    public function buy()
    {
        $user_id=session('user_id');
        $id=$_POST['id'];
        $count=$_POST['count'];
        $priceTotal=$_POST['priceTotal'];
        $date=$_POST['date'];
        $data=[
          'user_id'=>$user_id,
          'product_number'=>$id,
          'product_amount'=>$count,
          'start_time'=>$date,
          'product_subtotal'=>$priceTotal,
          'status'=>'1',
          'shopcart_time'=>time()
          
         ];

          $res=db('shopcart')->insert($data);
          $shopcartId = db('shopcart')->getLastInsID();
          $order_number = date('Ymd').substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);

          $ret[0]=$shopcartId;
          $ret[1]=$order_number;
          echo json_encode($ret);

       
    }
}